//
//  PYImageGalleryBarButtonItem.m
//  PictureYourself
//
//  Created by Daniel on 31/7/12.
//
//

#import "PYImageGalleryButton.h"

@implementation PYImageGalleryButton

-(id)init
{
    if(!(self = [super init]))
        return nil;
    
    thumbnail_ = nil;
    orientation_to_apply_ = 0;
    
    return self;
}

-(void)drawRect:(CGRect)rect
{
    //NSLog(@"Rect: %f, %f, %f, %f\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect clip_rect = rect;
    clip_rect.size.width -= 2;
    clip_rect.size.height -= 2;
    clip_rect.origin.x += 1;
    clip_rect.origin.y += 1;
    
    CGMutablePathRef clip_path = CreateRoundRectPath(context, clip_rect);
    CGContextClip(context);
    
    if(self.thumbnail)
    {
        UIImage* img = [UIImage imageWithCGImage:thumbnail_ scale:1.0 orientation:orientation_to_apply_];
        [img drawInRect:rect];
    }
    
    CGMutablePathRef shadow_path = CreateRoundRectPath(context, rect);
    DrawInnerShadow(context, shadow_path, 1.5);
    CGPathRelease(clip_path);
    CGPathRelease(shadow_path);
    
    if(self.state == UIControlStateHighlighted)
        NSLog(@"Gallery button is highlighted\n");
}

-(void)setThumbnail:(CGImageRef)thumbnail:(UIImageOrientation)orientation
{
    thumbnail_ = thumbnail;
    orientation_to_apply_ = orientation;
    CGImageRetain(thumbnail_);
    [self setNeedsDisplay];
}

-(CGImageRef)thumbnail
{
    return thumbnail_;
}

-(void)dealloc
{
    CGImageRelease(thumbnail_);
    [super dealloc];
}

@end
